import os
import numpy as np
from utils.polygon.ConvexHull import ConvexHull
from utils.polygon.Polygon import Polygon



    
if __name__ == "__main__":
    
    path = '/Users/tpilanta/Documents/workspace/python/ua-bb-model-framework-2013'
    polfile = os.path.join(path,'pol.txt')
    outlist = []
    
    """
    Cross walk boundaries are 2*(70,221), 2*(180,445), 2*(0,294), 2*(109,538)
    """
    gravel = Polygon(2*np.array([(0,0), (0,248), (244,0)]))                                             # gravel (top left triangle)
    crosswalk = Polygon(2*np.array([(70,221), (180,445), (0,294), (109,540)]))                          # cross-walk
    street = Polygon(2*np.array([(0,294), (70,221), (285,0), (525,0), (180,445), (109,540), (0,540)]))  # street
    topsidewalk = Polygon(2*np.array([(0,248), (244,0), (0,294), (285,0)]))                             # top sidewalk
    bottomsidewalk = Polygon(2*np.array([(525,0), (109,540), (605,0), (365,540)]))                      # bottom sidewalk
    bottombuilding = Polygon(2*np.array([(605,0), (365,540), (720,0), (720,540)]))                      # bottom building    
    
#    count = 0
    with open(polfile, 'r') as f:
        content = f.readlines()
        for row in content:
#            if count > 500:
#                break
            row = row.strip().split(" ")
            actorid = row[0]
            xmin = row[1]
            ymin = row[2]
            xmax = row[3]
            ymax = row[4]
            frame = row[5]
            valid = row[6]  # if 0, then valid
            occluded = row[7]
            generated = row[8]
            label = row[9]
            
            if int(valid) == 1:
                continue
            
            h = 30  # height of bounding box
            tl = (int(xmin),max(int(ymax)-h, int(ymin)))
            tr = (int(xmax),max(int(ymax)-h, int(ymin)))
            br = (int(xmax),int(ymax))
            bl = (int(xmin),int(ymax))
            abox = Polygon([tl,tr,br,bl])
            alist = [actorid,xmin,ymin,xmax,ymax,frame,valid,occluded,generated,label]
            
            if abox.collidepoly(gravel):
                alist.append("\"topgravel\"")
            if abox.collidepoly(topsidewalk):
                alist.append("\"topsidewalk\"")
            if abox.collidepoly(crosswalk):
                alist.append("\"crosswalk\"")
            if abox.collidepoly(street):
                alist.append("\"street\"")
            if abox.collidepoly(bottomsidewalk):
                alist.append("\"bottomsidewalk\"")
            if abox.collidepoly(bottombuilding):
                alist.append("\"bottomgravel\"")
            
            outlist.append(alist)
            
#            count += 1
    
    outfile = open(os.path.join(path,'pol_withregions.txt'), "w")
    for row in outlist:
        outfile.write(' '.join([str(item) for item in row]))
        outfile.write("\n")
    outfile.close()
    
               
                
            
            
            
                
            
            
            
            
            
            
            
